package com.arpa.ntocc.basic.api.controller;


import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.cache.PartyGroupCacheDO;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.controller.BaseController;
import com.arpa.ntocc.common.domain.dto.UserLoginCreateDTO;
import com.arpa.ntocc.common.domain.dto.UserLoginSearchDTO;
import com.arpa.ntocc.common.domain.vo.UserLoginVO;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.ntocc.common.security.annotation.NoRepeat;
import com.arpa.ntocc.common.service.IUserLoginService;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 账号管理 Controller
 * 用于后台账号管理
 * </p>
 *
 * @author liuxr
 * @since 2020-03-15
 */
@Api(tags = "账号管理")
@RestController
@RequestMapping(value = "/userLogin", produces = "application/json;charset=UTF-8")
@Log4j2
public class UserLoginController extends BaseController {


    private final IUserLoginService userLoginService;

    public UserLoginController(IUserLoginService userLoginService) {
        this.userLoginService = userLoginService;
    }


    /**
     * 获取 账号管理 列表
     *
     * @param searchDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 账号管理 列表", notes = "获取 账号管理 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = UserLoginVO.class))
    @NeedLogin
    @GetMapping(value = "")
    public Result list(UserLoginSearchDTO searchDTO, HttpServletRequest request) {
        //获取登录用户所属机构
        PartyGroupCacheDO partyGroupCacheDO = UserUtil.getBranch();
        if (partyGroupCacheDO.getLevel() > 0) {
            searchDTO.setBranchCode(partyGroupCacheDO.getCode());
        }

        UserLoginVO userLoginVOSum = userLoginService.queryListSum(searchDTO);
        List<UserLoginVO> userLoginVOS = userLoginService.queryList(searchDTO);

        Page returnPage = new Page(searchDTO.getPageNum(), searchDTO.getPageSize());
        returnPage.setRecords(userLoginVOS);
        returnPage.setTotal(userLoginVOSum.getTotal());
        returnPage.setSumObject(userLoginVOSum);
        return Result.ok().data(returnPage);
    }


    /**
     * 添加 登录账号
     *
     * @param entity  PartyDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 登录账号 ", notes = "添加 登录账号 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
    })
    @NeedLogin
    @NoRepeat(time = 2000)
    @PostMapping()
    public Result create(@RequestBody @Valid UserLoginCreateDTO entity, HttpServletRequest request) {
        entity.setPartyCode(UserUtil.getBranchCode());
        userLoginService.save(entity);
        return Result.ok("账号添加成功");
    }


    /**
     * 修改登录账号
     *
     * @param entity  用户
     * @param request
     * @return
     */
    @ApiOperation(value = "修改登录账号", notes = "修改登录账号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
    })
    @NeedLogin
    @PutMapping()
    public Result update(@RequestBody @Valid UserLoginCreateDTO entity, HttpServletRequest request) {
        userLoginService.update(entity);
        return Result.ok("账号添加成功");
    }


    /**
     * 获取登录账号信息
     *
     * @param userLoginId
     * @param request
     * @return
     */
    @ApiOperation(value = "获取登录账号信息", notes = "获取登录账号信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "userLoginId", value = "登录账号", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = UserLoginVO.class))
    @NeedLogin
    @GetMapping(value = "/{userLoginId}")
    public Result party(@PathVariable String userLoginId, HttpServletRequest request) {
        UserLoginVO userLoginVO = userLoginService.getByUserLoginId(userLoginId);
        return Result.ok().data(userLoginVO);
    }

    /**
     * 删除 登录账号
     *
     * @param userLoginId
     * @param request
     * @return
     */
    @ApiOperation(value = "删除登录账号 ", notes = "删除 登录账号 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "userLoginId", value = "登录账号", paramType = "path", dataType = "String")
    })
    @NeedLogin
    @DeleteMapping(value = "/{userLoginId}")
    public Result delete(@PathVariable String userLoginId, HttpServletRequest request) {
        userLoginService.removeByUserLoginId(userLoginId);
        return Result.ok("删除成功");
    }






}
